package com.yesjpt.web;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.yesjpt.domain.City;
import com.yesjpt.serv.CityServ;
import com.yesjpt.util.ContextUtil;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/city")
public class CityController {

    @Autowired private CityServ serv;

    // 根据城市名称查询/city?findByName?name=成都
    @GetMapping("/findByName")
    public List<City> findByName(String name) {
        System.out.println(new Throwable().getStackTrace()[0] + "， name = " + name);
        return serv.findByName(name);
    }

    @GetMapping("/findById")
    public City findById(HttpServletRequest request, String id) {
        //System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + 
        request.getParameter("id"));
        City vo = serv.findById(id);
        return vo;
    }
    // 根据ID查询，http://localhost:8086/city/1
    @GetMapping("/{id}")
    public City getOne(@PathVariable String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        City vo = serv.findById(id);
        return vo;
    }

    // 用delete方法根据ID删除
    @DeleteMapping("/{id}")
    public JSONObject delete(@PathVariable String id) {
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        return serv.deleteById(id);
    }

    // 用get方法根据ID删除
    @GetMapping("/deleteById")
    public JSONObject deleteById(String id) {
        System.out.println(new Throwable().getStackTrace()[0] + ", id = " + id);
        return serv.deleteById(id);
    }


    // 新增
    @PostMapping("/add")
    public JSONObject add(@RequestBody City vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return serv.add(vo);
    }

    // 更新
    @PutMapping("/update")
    public JSONObject update(@RequestBody City vo) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return serv.update(vo);
    }

    // 列出全部http://localhost:8886/city/listAll
    @GetMapping("/listAll")
    public List<City> listAll() {
        System.out.println(new Throwable().getStackTrace()[0]);
        return serv.listAll();
    }

    // 列出全部http://localhost:8886/city/listByParams
    @GetMapping("/listByParams")
    public List<City> listByParams(HttpServletRequest request) {
    	Map<String, Object> params = ContextUtil.getParams(request);
        System.out.println(new Throwable().getStackTrace()[0] + ", params = " + params);
    	/*
    	System.out.println("***cond = " + request.getParameter("cond"));
    	System.out.println("***orderBy = " + request.getParameter("orderBy"));
    	System.out.println("***provinceId = " + request.getParameter("provinceId"));

        */
        JSONObject p = new JSONObject();
        p.putAll(params);
        System.out.println("p = " + p);
        return serv.listByParams(params);
    }

}




























